package com.hou.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hou.eduservice.entity.EduSubject;
import com.hou.eduservice.entity.excel.SubjectData;
import com.hou.eduservice.entity.subject.OneSubject;
import com.hou.eduservice.entity.subject.TwoSubject;
import com.hou.eduservice.listener.SubjectExcelListener;
import com.hou.eduservice.mapper.EduSubjectMapper;
import com.hou.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-13
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    // 添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            // 文件输入流
            InputStream in = file.getInputStream();

            // 读取数据
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();

        } catch (Exception e){
            e.printStackTrace();
        }

    }

    // 树形结构获取课程
    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        QueryWrapper<EduSubject> queryWrapperOne = new QueryWrapper<>();
        queryWrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(queryWrapperOne);

        QueryWrapper<EduSubject> queryWrapperTwo = new QueryWrapper<>();
        queryWrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(queryWrapperTwo);

        // 创建集合整合一二级分类并返回数据
        ArrayList<OneSubject> list = new ArrayList<>();

        // 封装一级分类
        // 循环遍历oneSubjectList放入list中

        for (int i = 0; i < oneSubjectList.size(); i++) {
            // 得到每个一个分类
            EduSubject subject = oneSubjectList.get(i);

            // 将一级分类取出并放入带有二级分类框架的一级分类集合中
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(subject,oneSubject);

            // 处理二级分类
            // 二级分类集合
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();

            for (int j = 0; j < twoSubjectList.size(); j++) {
                // 获取每一个二级分类
                EduSubject subjectChildren = twoSubjectList.get(j);
                // 判断是否为一级分类的子类
                if (subjectChildren.getParentId().equals(subject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(subjectChildren,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }

            oneSubject.setChildren(twoFinalSubjectList);

            // 将一级分类加入新的集合中
            list.add(oneSubject);

        }

        return list;
    }
}
